//============================================================================
// Name        : CapaN-Distribuidos.cpp
// Author      : 
// Version     :
// Copyright   : 
//============================================================================

#include <iostream>
#include <cstring>
#include "../IPC/SizedMessageQueue.h"
#include "../middleware/MiddlewareCommon.h"
#include "../channel_common/ChannelCommon.h"
#include "../CapaServiciosDistribuidos/Protocolo.h"
#include "../logger/logger.h"
#include "../channel_common/ChannelInterface.h"

using namespace std;
using namespace channel;

/*
 *  Componente de capa N : Receptor general
 *
 */
int main(int argc, char* argv[]) {
	CLogger::log("Esperando mensaje de la capa de comunicaciones.", LogLevelEnum::INFO);

	ipc::MessageQueue<PayloadCapaServicios> colaCapaServicios = ipc::MessageQueue<PayloadCapaServicios>::create(COLA_SERVICIOS_PATH, COLA_SERVICIOS_KEY);
	ChannelInterface channel(ChannelInterface::RECEIVER);

	bool conexionTerminada = false;
	while(!conexionTerminada) {
		char payloadChannel[TAM_PDU_CAPAN];
		channel.recv(payloadChannel,TAM_PDU_CAPAN);

		PDUCapaServicios pduServicios;
		memcpy((void*)&pduServicios, (const void*)payloadChannel, TAM_PDU_CAPAN);
		colaCapaServicios.put(pduServicios.tipo, pduServicios.payload);
	}

	ipc::MessageQueue<PayloadCapaServicios>::destroy(COLA_SERVICIOS_PATH, COLA_SERVICIOS_KEY);
}
